<?php
/**
* Function: 
*	This is the postback page for the join.php page.
*
* @uses includes.php
* @package joiner
* @param string $redirect_url OUTPUT.  VIP -- used by action_redirect to move to the proper page.
**/

	// Note, this is called by files in /mod/joiner/html/, and not from the folder that this page is actually residing inside of.
	require_once('../../../includes.php');
    global $db, $CFG;
    global $messages;

    $user_ident = $_SESSION['userid'];
    
    // Start loading new variables
    foreach ( $JOINER_CFG->fields as $field ) {
        // Load name
        $argument_name = $field['name'];
        $argument_value = required_param( 'joiner_' . $field['name'] );
        
        // Branch on type of the field.
        if ( $field['type'] == 'select' ) {
            // selection.  Ensure that we match a valid entry.
            if ( !in_array( $argument_value, $field['values'] ) ) {
                error(__gettext("Invalid argument {$argument_value} passed for joiner_update_post.php" .
                    " field {$field['name']}.  Please select from the available options"));
                die();
            }
        } elseif ( $field['type'] == 'input' ) { 
            // No check.
        } else { 
            // error
            die('Unknown type value passed to join.php from /mod/joiner/config/php');
        }
        
        // Move argument into db
        joiner_load_argument( $user_ident, $argument_name, $argument_value );
    }    
    
	// Set redirect
	$redirect_url = url; 
   
    // Flush & recheck the membership rules.
    joiner_checkmemberships( $_SESSION['userid'] );
    
    
/**
* Called to set a joiner argument in the db
**/
function joiner_load_argument( $user_ident, $argument_name, $argument_value ) {

    // Delete previous values.
    delete_records('profile_data','name',$argument_name,'owner',$user_ident);
    
    // Add argument to mysql db.
    $arg = new StdClass;
    $arg->owner = $user_ident;
    $arg->name = $argument_name;
    $arg->value = $argument_value;
    $arg->acess = 'PUBLIC';
    insert_record('profile_data',$arg);
}	
?>